package com.saftalk.ui;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.saftalk.db.entity.Family;
import com.saftalk.db.entity.User;

public class UIUserResult extends UIResult implements Serializable {

	private static final long serialVersionUID = 1L;
	private static final int DEFAULT_FONT_SIZE = 1;
	private static final boolean DEFAULT_KEYBOARD_ENABLED = false;

	
	private final String name;
	private final String channel;
	private final Long userId;
	private final Long familyId;
	private final String photoId;
	private final boolean isSenior;
	private final boolean isAdmin;
	private final String generatedPassword;
	private final String phoneNumber;
	private final String email;
	private boolean keyboardEnabled;
	private Integer fontSize;

	public UIUserResult(User user, Family family) throws NoSuchAlgorithmException {
		
		if (user == null) {
			this.success = false;
			this.name = null;
			this.channel = null;
			this.userId = null;
			this.familyId = null;
			this.photoId = null;
			this.isSenior = false;
			this.isAdmin = false;
			this.generatedPassword = null;
			this.phoneNumber = null;
			this.email = null;			
		} else {
			this.success = true;
			this.name = user.getName();
			this.channel = getChannelByMD5(user.getFamilyId());
			this.userId = user.getId();
			this.familyId = user.getFamilyId();
			this.photoId = user.getPhotoId();
			this.isSenior = user.isSenior();
			this.isAdmin = user.isAdmin();
			this.generatedPassword = user.getGeneratedPassword();
			this.phoneNumber = user.getPhoneNumber();
			this.email = user.getEmail();
		}
		if (family == null) {
			this.keyboardEnabled = DEFAULT_KEYBOARD_ENABLED;
			this.fontSize = DEFAULT_FONT_SIZE;
		} else {
			this.keyboardEnabled = (family.getKeyboardEnabled() == null) ? DEFAULT_KEYBOARD_ENABLED : family.getKeyboardEnabled();
			this.fontSize = (family.getFontSize() == null) ? DEFAULT_FONT_SIZE : family.getFontSize();
		}
	
	
	}
	
	public UIUserResult(User user) throws NoSuchAlgorithmException {

		this(user, null);
	}



	private String getChannelByMD5(Long id) throws NoSuchAlgorithmException {

		MessageDigest md = MessageDigest.getInstance("MD5");
		byte[] channel = md.digest(Long.toString(id).getBytes());
		StringBuffer hexString = new StringBuffer();
		for (int i = 0; i < channel.length; i++) {
			hexString.append(Integer.toHexString(0xFF & channel[i]));
		}
		return hexString.toString();
	}

	public String getName() {
		return name;
	}

	public String getChannel() {
		return channel;
	}
	
	public Long getUserId() {
		return userId;
	}

	public Long getFamilyId() {
		return familyId;
	}
	
	public String getPhotoId() {
		return photoId;
	}
	
	public Boolean isSenior() {
		return isSenior;
	}
	
	public boolean isAdmin() {
		return isAdmin;
	}
	
	public String getGeneratedPassword() {
		return generatedPassword;
	}
	
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public String getEmail() {
		return email;
	}



	public boolean isKeyboardEnabled() {
		return keyboardEnabled;
	}


	public Integer getFontSize() {
		return fontSize;
	}
	
	

}
